04. SQL
SQL
ND004 C01 L02 04 SQL
SQL Review
You should feel familiar and comfortable with each of the commands below. If you feel like you need a review or some practice with any of these, you can find an introduction to SQL in our free Intro to Relational Databases course.
You can also click on any of the commands to check out the Postgres tutorial page on how that command works.
Manipulating Data
Querying Data
Structuring Data
Joins & Groupings
SQL Quiz 1
SOLUTION:
TrueSOLUTION:
DialectSQL Quiz 2
SOLUTION:
SELECTSQL Practice Exercises
Go to this SQLFiddle of drivers and vehicles , and try practicing SQL using the exercises below.
Manipulating & Querying Data
- Insert a few records into both drivers and vehicles. Include 3 records of drivers who have vehicles, belonging in the vehicles table.
- Select all driver records; select all vehicle records; select only 3 vehicle records (using LIMIT )
- Driver with ID 2 no longer owns any vehicles. Update the database to reflect this.
- Driver with ID 1 now owns a new vehicle in addition to the previous one they owned. Update the database to reflect this.
Joins & Group Bys
- Select all vehicles owned by driver with ID 3.
- Select all vehicles owned by driver with name 'Sarah' (without knowing their ID).
- Show a table of the number of vehicles owned per driver.
- Show the number of drivers that own a Nissan model.
Structuring Data
- Add information about vehicle color.
- Update all existing vehicle records to have a vehicle color.
- Add contact information (email, address) to the drivers table.
Challenges
Using Timestamps (see help here) ,
- Update vehicles table to show date of registration information
- The DMV is looking to notify all drivers with a vehicle that needs their registration renewed in the next month. If vehicles need to renew their vehicles once every year after their date of registration, then write a query to fetch all drivers with at least 1 vehicle that has an upcoming renewal in the next month, fetching their contact information as well as information about which vehicles need renewals. The DMV would like to run this query every time they need to contact all drivers that have an upcoming renewal in the next month.